package com.sky.task;

import com.sky.context.BaseContext;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    WebSocketServer webSocketServer;

    /**
     * 通过WebSocket每隔5秒向客户端发送消息
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    public void sendMessageToClient() {
        webSocketServer.sendToAllClient("这是来自服务端的消息：" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
    }


    /**
     * 每分钟:处理支付超时的订单
     */
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeOutOrder(){
        //查询订单，条件：状态=待付款，下单时间
        log.info("处理支付超时的订单:{}",LocalDateTime.now());
        //查询未支付状态，下单时间<15分钟前的订单
        Orders o = Orders.builder().status(Orders.PENDING_PAYMENT)
                .orderTime(LocalDateTime.now().plusMinutes(-15))
                .id(BaseContext.getCurrentId()).build();
        List<Orders> orders = orderMapper.getOrders(o);
        //修改查询到的订单：状态、取消原因、取消时间
        if (orders!=null && orders.size()>0){
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                order.setCancelReason("订单超时未支付,自动取消");
                order.setCancelTime(LocalDateTime.now());
                //修改
                log.info("支付超时，自动取消，订单id:{}",order.getNumber());
                orderMapper.update(order);
            }
        }
    }

    /**
     * 每天0点打烊:处理12点前未支付的订单
     */
    //@Scheduled(cron = "0 0 0 * * ?")
    public void processDayOutOrder(){
        log.info("处理12点前未支付的订单:{}",LocalDateTime.now());
        //查询订单，条件：状态=待付款1，下单时间<当前时间
        Orders o = Orders.builder().status(Orders.PENDING_PAYMENT)
                .orderTime(LocalDateTime.now())
                .id(BaseContext.getCurrentId()).build();
        List<Orders> orders = orderMapper.getOrders(o);
        //修改查询到的订单：状态、取消原因、取消时间
        if (orders!=null && orders.size()>0){
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                order.setCancelReason("店铺已打烊");
                order.setCancelTime(LocalDateTime.now());
                //修改
                orderMapper.update(order);
            }
        }
    }

    /**
     * 每天凌晨1点:处理派送中的订单
     */
    //@Scheduled(cron = "0 0 1 * * ?")
    public void processFinishedOrder(){
        log.info("处理派送中的订单，每天1点整，将派送中的订单:{}",LocalDateTime.now());
        //查询订单，条件：状态=派送中4，下单时间<当前时间-1h
        Orders o = Orders.builder().status(Orders.DELIVERY_IN_PROGRESS)
                .orderTime(LocalDateTime.now().plusMinutes(-60))
                .id(BaseContext.getCurrentId()).build();
        List<Orders> orders = orderMapper.getOrders(o);
        //修改查询到的订单：状态
        if (orders!=null && orders.size()>0){
            for (Orders order : orders) {
                order.setStatus(Orders.COMPLETED);
                //修改
                orderMapper.update(order);
            }
        }
    }

}

















